//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Text.RegularExpressions;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
namespace DotNetNuke.UI.UserControls
{
	public abstract class ModuleAuditControl : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.Label lblCreatedBy;
		protected System.Web.UI.WebControls.Label lblUpdatedBy;
		private string _CreatedDate = "";
		private string _CreatedByUser = "";
		private string _LastModifiedByUser = string.Empty;
		private string _LastModifiedDate = string.Empty;
		private DotNetNuke.Entities.BaseEntityInfo _Entity;
		private string _SystemUser = string.Empty;
		private string MyFileName = "ModuleAuditControl.ascx";
		public string CreatedDate
		{
			set { _CreatedDate = value; }
		}
		public string CreatedByUser
		{
			set { _CreatedByUser = value; }
		}
		public string LastModifiedByUser
		{
			set { _LastModifiedByUser = value; }
		}
		public string LastModifiedDate
		{
			set { _LastModifiedDate = value; }
		}
		public DotNetNuke.Entities.BaseEntityInfo Entity
		{
			set { _Entity = value; }
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			try
			{
				if (_Entity != null)
				{
					_CreatedByUser = _Entity.CreatedByUserID.ToString();
					_CreatedDate = _Entity.CreatedOnDate.ToString();
					_LastModifiedByUser = _Entity.LastModifiedByUserID.ToString();
					_LastModifiedDate = _Entity.LastModifiedOnDate.ToString();
				}
				bool isCreatorAndUpdater = false;
				if (Regex.IsMatch(_CreatedByUser, "^\\d+$") && Regex.IsMatch(_LastModifiedByUser, "^\\d+$") && _CreatedByUser == _LastModifiedByUser)
					isCreatorAndUpdater = true;
				_SystemUser = Services.Localization.Localization.GetString("SystemUser", Services.Localization.Localization.GetResourceFile(this, MyFileName));
				ShowCreatedString();
				ShowUpdatedString(isCreatorAndUpdater);
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private string ShowCreatedString()
		{
			if (Regex.IsMatch(_CreatedByUser, "^\\d+$"))
			{
				if (int.Parse(_CreatedByUser) == Null.NullInteger)
				{
					_CreatedByUser = _SystemUser;
				}
				else
				{
					UserController objUsers = new UserController();
					UserInfo objUser = UserController.GetUserById(PortalController.GetCurrentPortalSettings().PortalId, int.Parse(_CreatedByUser));
					if (objUser != null)
					{
						_CreatedByUser = objUser.DisplayName;
					}
				}
			}
			string str = Services.Localization.Localization.GetString("CreatedBy", Services.Localization.Localization.GetResourceFile(this, MyFileName));
			lblCreatedBy.Text = string.Format(str, _CreatedByUser, _CreatedDate);
			return str;
		}
		private void ShowUpdatedString(bool isCreatorAndUpdater)
		{
			if (string.IsNullOrEmpty(_LastModifiedDate))
				return;
			string str = string.Empty;
			if (Regex.IsMatch(_LastModifiedByUser, "^\\d+$"))
			{
				if (isCreatorAndUpdater == true)
				{
					_LastModifiedByUser = _CreatedByUser;
				}
				else if (int.Parse(_LastModifiedByUser) == Null.NullInteger)
				{
					_LastModifiedByUser = _SystemUser;
				}
				else
				{
					UserController objUsers = new UserController();
					UserInfo objUser = UserController.GetUserById(PortalController.GetCurrentPortalSettings().PortalId, int.Parse(_LastModifiedByUser));
					if (objUser != null)
					{
						_LastModifiedByUser = objUser.DisplayName;
					}
				}
			}
			str = Services.Localization.Localization.GetString("UpdatedBy", Services.Localization.Localization.GetResourceFile(this, MyFileName));
			lblUpdatedBy.Text = string.Format(str, _LastModifiedByUser, _LastModifiedDate);
		}
	}
}
